package com.btex.app.treaty.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.btex.app.Config;
import com.btex.app.R;
import com.btex.app.entity.BaoZhengJingInfo;
import com.btex.app.tools.MicroAVLIDialog;
import com.btex.app.tools.Util;
import com.btex.app.treaty.DecimalDigitsInputFilter;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/15
 * @time 下午6:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class EarnestDialog extends Dialog {

    @BindView(R.id.dialog_traety_unwind_imag_close)
    ImageView mDialogTraetyUnwindImagClose;
    @BindView(R.id.dialog_traety_earnest_tv_need)
    TextView mDialogTraetyEarnestTvNeed;
    @BindView(R.id.dialog_traety_earnest_tv_reduce)
    TextView mDialogTraetyEarnestTvReduce;
    @BindView(R.id.dialog_traety_earnest_ed_number)
    EditText mDialogTraetyEarnestEdNumber;
    @BindView(R.id.dialog_traety_earnest_tv_money)
    TextView mDialogTraetyEarnestTvMoney;
    @BindView(R.id.dialog_traety_earnest_tv_sure)
    TextView mDialogTraetyEarnestTvSure;
    @BindView(R.id.dialog_traety_earnest_tv_type)
    TextView mDialogTraetyEarnestTvType;
    @BindView(R.id.jiazai)
    ImageView mJiazai;
    private Context mContext;
    private String mPosition_id;
    private String mValuation;
    private String mPrice_float_num = "4";
    private String type = "1";
    private MicroAVLIDialog mMicroAVLIDialog;
    private BaoZhengJingInfo mBaoZhengJingInfo;

    public EarnestDialog(@NonNull Context context, String position_id, String valuation, String Price_float_num) {
        super(context, R.style.coupon_style);
        mContext = context;
        mPosition_id = position_id;
        mValuation = valuation;
        mPrice_float_num = Price_float_num;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.earnest_dialog);
        ButterKnife.bind(this);
        Glide.with(getContext()).load(R.drawable.jiazai).into(mJiazai);
        mDialogTraetyEarnestEdNumber.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(Integer.valueOf(mPrice_float_num))});

        getValue();

        mMicroAVLIDialog = new MicroAVLIDialog(getContext());
        mDialogTraetyEarnestTvNeed.setSelected(true);
        mDialogTraetyEarnestTvType.setText(mValuation);
        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 0.9);
        attributes.height = (int) (heightPixels * 0.45);
        //让Dialog位置居中
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);

    }


    @OnClick({R.id.dialog_traety_unwind_imag_close, R.id.dialog_traety_earnest_tv_need, R.id.dialog_traety_earnest_tv_reduce, R.id.dialog_traety_earnest_tv_sure, R.id.dialog_traety_earnest_tv_sure_lin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_traety_unwind_imag_close:
                dismiss();
                break;
            case R.id.dialog_traety_earnest_tv_need:
                mDialogTraetyEarnestEdNumber.setHint(R.string.heyue_add_jian_type);
                type = "1";
                SetStat();
                mDialogTraetyEarnestTvNeed.setSelected(true);

                if (mBaoZhengJingInfo != null) {

                    mDialogTraetyEarnestTvMoney.setText(mContext.getString(R.string.zui_jia_duo) + mBaoZhengJingInfo.getData().getMax_in() + "\t" + mValuation);
                }


                break;
            case R.id.dialog_traety_earnest_tv_reduce:
                mDialogTraetyEarnestEdNumber.setHint(R.string.heyue_add_jian_type);

                if (mBaoZhengJingInfo != null) {
                    Log.i("woefpwoefpwoefw", "2");
                    mDialogTraetyEarnestTvMoney.setText(mContext.getString(R.string.zui_jia_shao) + mBaoZhengJingInfo.getData().getMax_out() + "\t" + mValuation);
                }

                type = "-1";
                SetStat();
                mDialogTraetyEarnestTvReduce.setSelected(true);

                break;
            case R.id.dialog_traety_earnest_tv_sure_lin:
                mDialogTraetyEarnestTvSure.setClickable(false);

                String s = mDialogTraetyEarnestEdNumber.getText().toString();
                if (!TextUtils.isEmpty(s)) {
                    setAdjustPositionMargin(s);
                } else {
                    mDialogTraetyEarnestTvSure.setClickable(true);
                    Toast.makeText(getContext(), "数据不能为空", Toast.LENGTH_SHORT).show();
                }


                break;
        }
    }

    private void setAdjustPositionMargin(String number) {


        int num = (int) ((Math.random() * 9 + 1) * 100000);
        mJiazai.setVisibility(View.VISIBLE);
        if (!type.equals("1")) {

            number = "-" + number;
        }

        OkGo.<String>post(Config.ADJUST_POSITION_MARGIN).params("position_id", mPosition_id).params("delta_m", number).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                String result = response.body().toString();

                Log.i("ikmbhyuuu", result);
                mDialogTraetyEarnestTvSure.setClickable(true);
                mJiazai.setVisibility(View.GONE);

                try {
                    JSONObject jsonObj = new JSONObject(result);
                    if (jsonObj.getString("code").equals("1")) {

                        Toast.makeText(getContext(), "操作成功", Toast.LENGTH_SHORT).show();
                        dismiss();

                    } else {
                        Util.showSnachBarDialog(mDialogTraetyUnwindImagClose, getContext().getResources().getColor(R.color.red), jsonObj.getString("info"));
                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }
        });


//        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
//        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("position_id", mPosition_id);
//        params.addBodyParameter("delta_m", number);

//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.ADJUST_POSITION_MARGIN, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                Log.i("ikmbhyuuu", responseInfo.result);
//                mDialogTraetyEarnestTvSure.setClickable(true);
//                mJiazai.setVisibility(View.GONE);
//
//                try {
//                    JSONObject jsonObj = new JSONObject(responseInfo.result);
//                    if (jsonObj.getString("code").equals("1")) {
//
//                        Toast.makeText(getContext(), "操作成功", Toast.LENGTH_SHORT).show();
//                        dismiss();
//
//                    } else {
//
////                        Snackbar snackbar = Util.ShortSnackbar(mDialogTraetyEarnestEdNumber, jsonObj.getString("info"), Color.parseColor("#ffffff"), Color.parseColor("#272736"));
////                        snackbar.show();
//                        Util.showSnachBarDialog(mDialogTraetyUnwindImagClose, getContext().getResources().getColor(R.color.red), jsonObj.getString("info"));
//                    }
//
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//
//                }
//
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//
//        });

    }


    public void SetStat() {
        mDialogTraetyEarnestTvNeed.setSelected(false);
        mDialogTraetyEarnestTvReduce.setSelected(false);
    }


    private void getValue() {

        int num = (int) ((Math.random() * 9 + 1) * 100000);

        OkGo.<String>post(Config.DELTA_MARGIN_RANGE).params("position_id", mPosition_id).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();
                Log.i("hewfjwefy", result);

                try {
                    JSONObject jsonObj = new JSONObject(result);
                    if (jsonObj.getString("code").equals("1")) {

                        Gson gson = new Gson();
                        mBaoZhengJingInfo = gson.fromJson(result, BaoZhengJingInfo.class);
                        String max_in = mBaoZhengJingInfo.getData().getMax_in();
                        Log.i("wlekfjwliejfow", max_in);

                        mDialogTraetyEarnestTvMoney.setText(mContext.getString(R.string.zui_jia_duo) + mBaoZhengJingInfo.getData().getMax_in() + "\t" + mValuation);


                    } else {

                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }
        });


//        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
//        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("position_id", mPosition_id);
//
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.DELTA_MARGIN_RANGE, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                Log.i("hewfjwefy", responseInfo.result);
//
//                try {
//                    JSONObject jsonObj = new JSONObject(responseInfo.result);
//                    if (jsonObj.getString("code").equals("1")) {
//
//                        Gson gson = new Gson();
//                        mBaoZhengJingInfo = gson.fromJson(responseInfo.result, BaoZhengJingInfo.class);
//                        String max_in = mBaoZhengJingInfo.getData().getMax_in();
//                        Log.i("wlekfjwliejfow", max_in);
//
//                        mDialogTraetyEarnestTvMoney.setText(mContext.getString(R.string.zui_jia_duo) + mBaoZhengJingInfo.getData().getMax_in() + "\t" + mValuation);
//
//
//                    } else {
//
//                    }
//
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//
//                }
//
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//
//        });

    }


//    DELTA_MARGIN_RANGE

}
